package com.atguigu.Listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class MyMessageListener {

    public static final String EXCHANGE_DIRECT = "exchange.direct.order";
    public static final String ROUTING_KEY = "order";
    public static final String QUEUE_NAME = "queue.order";
    private final HttpMessageConverters messageConverters;

    public MyMessageListener(HttpMessageConverters messageConverters) {
        this.messageConverters = messageConverters;
    }

    @RabbitListener(queues ={ "queue.order"})
    public void processMessagePrefech(String msg,Message message,Channel channel) throws IOException, InterruptedException {
        log.info("消费端接受到消息内容:"+msg);

        TimeUnit.MILLISECONDS.sleep(100);

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

    //采用注解进行创建队列,交换机并绑定,如果不存在创建,存在则必须一致,否则会报错
//    @RabbitListener(bindings = {
//            @QueueBinding(
//                    value =@Queue(name = QUEUE_NAME, durable = "true", autoDelete = "false",exclusive = "false"),
//                    exchange = @Exchange(name = EXCHANGE_DIRECT, durable = "true", autoDelete = "false", internal = "false"),
//                    key = {
//                            ROUTING_KEY
//                    }
//            )
//    })

    //手动确认有异常,为确认会重复拿消息死循环,一般要有次数限制
//    public void processMsg(String msg,Message message,Channel channel) throws IOException {
//
//        /**
//         * getMessageProperties  消息属性
//         * deliveryTag 投递标签,即消息id
//         */
//        long deliverTag = message.getMessageProperties().getDeliveryTag();
//
//        //没有异常,但是逻辑失败,自动确认消息会丢失
//        try {
//            log.info("msg"+msg);
//            int i = 1/0;
//            //无异常情况下,给rabbitMQ服务器返回ACK确认信息
//            /**
//             * 第二个参数:是一次性确认多个,这里false,只确认当前消息
//             */
//            channel.basicAck(deliverTag,false);
//        } catch (Exception e) {
//            //有异常,会判断是否是重复投递的信息
//            //redilivered消息是否是重复投递的
//            Boolean redelivered = message.getMessageProperties().getRedelivered();
//            if(!redelivered){ //首次投递失败,回到原有队列,可以再次消费
//                //requeue表示应该重回队列,而不是被丢弃
//                //basicNack N是negative,是不进行确认
//                channel.basicNack(deliverTag,false,true);
//            }else{//是重复投递的消息,不在处理了,直接丢弃或去到死信队列
//                //拒绝,不能再回到原来的队列
//                channel.basicReject(deliverTag,false);
//            }
//        }
//
//    }






















    //使用@RabbitListener注解设定要监听的队列名称
    @RabbitListener(queues = {"atguigu.queue.simple"})
    public void processMessage(String messageContent, Message message, Channel channel) {
        System.out.println("messageContent = " + messageContent);
    }

    @Value("server.port")
    String port;
    //测试工作队列,通过端口区分多个消费者
    @RabbitListener(queues = {"atguigu.queue.work"})
    public void ListenerWork(String messageContent, Message message, Channel channel) {
        System.out.println("message" + message);
        System.out.println("messageContent = " + messageContent);
    }
    //订阅模式都能收到消息
    @RabbitListener(queues = {"atguigu.queue.fanout01"})
    public void processMessage01(String messageCount,Message message,Channel channel){
        System.out.println("messageCount = " + messageCount);
    }
    @RabbitListener(queues = {"atguigu.queue.fanout02"})
    public void processMessage02(String messageCount,Message message,Channel channel){
        System.out.println("messageCount = " + messageCount);
    }
    //路由模型
    @RabbitListener(queues = "atguigu.queue.direct")
    public void processMessageRouting(String messageCount, Message message, Channel channel){
        System.out.println("messageCount = " + messageCount);
    }
    //主题模型
    @RabbitListener(queues = "atguigu.queue.message")
    public void processMessage03(String messageCount,Message message,Channel channel){
        System.out.println("messageCount = " + messageCount);

    }
    @RabbitListener(queues = "atguigu.queue.order")
    public void processMessage04(String messageCount,Message message,Channel channel){
        System.out.println("messageCount = " + messageCount);

    }
}
